#! /bin/bash
#set -x

PERL_PROCESS="/usr/local/bin/perl"
MAC_PROC_COUNT=25
USER=$USER
TR_LOG="/usr/local/scratch/$USER/test_track.log"
HOST=`hostname`
testname=test-wf-cron


# Count perl active process and skip new test, if the process count is more than 25.
count=`ps -aef | grep "$USER" | grep -c "$PERL_PROCESS"`
echo -n " Active perl process count is $count ." >> /usr/local/scratch/$USER/wf-test/submit.log
if [ $count -ge 25 ]; then 
	echo -n " Active perl process count exceeded 25. Skipping test." >> /usr/local/scratch/$USER/wf-test/submit.log
	TIME=`date`
	echo "$TIME : $HOST: Test $testname SKIPPED\n" >> $TR_LOG
	exit 1
fi

echo "$TIME : $HOST: Test $testname STARTED\n" >> $TR_LOG
echo -n "Starting workflow test at: " >> /usr/local/scratch/$USER/wf-test/submit.log
date >> /usr/local/scratch/$USER/wf-test/submit.log

# source workflow
source /usr/local/devel/ANNOTATION/workflow-test-htc/exec_env.bash

# change directory to
cd /usr/local/scratch/$USER/wf-test

# clear old xml file
rm sge_new.xml sleep.sh.* sge_new.xml.log

# RunWorkflow
RunWorkflow -i sge_new.xml -t sge_new_template.xml -c sge_new.ini

set ret=$?

echo "$TIME : $HOST: Test $testname FINISHED\n" >> $TR_LOG
echo -n "Finished workflow with return value $ret at: " >> /usr/local/scratch/$USER/wf-test/submit.log
date >> /usr/local/scratch/$USER/wf-test/submit.log

exit $ret
